#include <reg52.h>
#include <dht11.h>
#include <stdio.h>

// 74LS139译码器输入端
sbit A24 = P2 ^ 4;
sbit B24 = P2 ^ 5;

// 数码管显示数字（共阳）
unsigned char ledChar[] = {
		0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
		0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E};
unsigned char targetLed = 0;

int curMs = 0;
unsigned char ledsCode[8];
unsigned char flag1s = 0;
void main()
{
	DHT11_start();
	DHT11_receive();

	ledsCode[0] = rec_dat[1] - '0';
	ledsCode[1] = rec_dat[0] - '0';
	ledsCode[2] = rec_dat[5] - '0';
	ledsCode[3] = rec_dat[4] - '0';
	while (1)
	{
		curMs++;
		if (curMs >= 1000)
		{
			curMs = 0;
			flag1s = 1;
		}
		if (flag1s == 1)
		{
			flag1s = 0;
			// 虚假的一秒到了
			DHT11_receive();

			ledsCode[0] = rec_dat[1] - '0';
			ledsCode[1] = rec_dat[0] - '0';
			ledsCode[2] = rec_dat[5] - '0';
			ledsCode[3] = rec_dat[4] - '0';
		}

		if (curMs % 2 == 0)
		{
			P0 = 0;
			switch (targetLed)
			{
			case 0:
				B24 = 0;
				A24 = 0;
				targetLed++;
				P0 = ~ledChar[ledsCode[0]];
				break;
			case 1:
				B24 = 0;
				A24 = 1;
				targetLed++;
				P0 = ~ledChar[ledsCode[1]];
				break;
			case 2:
				B24 = 1;
				A24 = 0;
				targetLed++;
				P0 = ~ledChar[ledsCode[2]];
				break;
			case 3:
				B24 = 1;
				A24 = 1;
				targetLed = 0;
				P0 = ~ledChar[ledsCode[3]];
				break;
			default:
				break;
			}
		}
	}
}
